<?php
namespace app\api\controller;
use app\model\WxActivityField as WxActivityFieldModel;
use app\model\Token as TokenModel;
use think\Db;


class WxActivityField extends Base
{   
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index($wx_activity_id){
        $user_id = $this->login_user['user_id'];
        $map_params = [

        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('keyword_search')) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        $map[] = ['wx_activity_id','=',$wx_activity_id];

        $model = new WxActivityFieldModel();
        $list = $model->where($map)->order('id','asc')->select();

        $contents = Db::name('wx_uactivity')->where([
            ['user_id','=',$user_id],
            ['wx_activity_id','=',$wx_activity_id],
            ['status','<>',-1]
        ])->value('fields');
        if (empty($contents)) {
            $contents = [];
        }else{
            $contents = json_decode($contents,true);
            $contents = arrayParamExtract($contents,'name','value');
        }
        $uinfo = Db::name('user')->where('id',$user_id)->find();
        foreach ($list as $k => $vo) {
            if (in_array($vo['type'],['select','checkbox','datalist'])) {
                $list[$k]['params'] = explode(',',$vo['params']);
            }
            $value = isset($contents[$vo['name']]) ? $contents[$vo['name']] : (empty($vo['default']) ? '' : $vo['default']);
            

            if (empty($value) && $value != '0') {
                if (in_array($vo['name'], ['手机','电话','手机号码','手机号','联系电话']) && !empty($uinfo['mobile'])) {
                    $value = $uinfo['mobile'];
                }elseif (in_array($vo['name'], ['姓名','真实姓名']) && !empty($uinfo['truename'])) {
                    $value = $uinfo['truename'];
                }
            }
            $list[$k]['value'] = $value;
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 保存
     * @Author   cch
     * @DateTime 2020-05-26T17:22:32+0800
     * @return   [type]                   [description]
     */
    public function update(){
        $user_id = $this->login_user['user_id'];
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        $fields = [
            'must'=>['wx_activity_id','fields']
        ];
        $params = paramFilter($param,$fields);
        if (!empty($params['error_msg'])) {
            return apiResult('5000',$params['error_msg']);
        }

        $content = $params['fields'];
        if (is_array($content)) {
            $content = json_encode($params['fields'],JSON_UNESCAPED_UNICODE);
        }

        $map = [
            ['user_id','=',$user_id],
            ['wx_activity_id','=',$params['wx_activity_id']]
        ];
        if ( Db::name('wx_uactivity_field')->where($map)->value('id') > 0 ) {
            Db::name('wx_uactivity_field')->where($map)->update([
                'content'=>$content
            ]);
        }else{
            Db::name('wx_uactivity_field')->where($map)->insert([
                'user_id'=>$user_id,
                'wx_activity_id'=>$params['wx_activity_id'],
                'content'=>$content,
                'ctime'=>time()
            ]);
        }


        // $survey_fields = json_decode($params['fields'],true);
        // $survey_fields = $params['fields'];

        // $model = new WxActivityFieldModel();
        // foreach ($survey_fields as $k => $vo) {
        //     $model->where([
        //         ['wx_activity_id','=',$params['wx_activity_id']],
        //         ['name','=',$vo['name']]
        //     ])->update(['value'=>$vo['value']]);
        // }




        return apiResult('2000','更新成功');
    }

   


}
